/// <reference path="./jquery-1.8.2.min.js" />
/// <reference path="./fourkill.util.js" />
/// <reference path="./fourkill.painters.js" />

var ss, canvas, backCanvas, ctx, bctx, sky, ground, pctx, mist, objs = [], inputHandler;
var ready = false;
var rotationCount = 0;
var isSensorReady = false;
var sillyRotate = 0, sillyRotateInterval = 1;

$(window).resize(function () {
    if (ready) {
        canvas.height = $(window).height();
        canvas.width = $(window).width();
        backCanvas.height = $(window).height();
        backCanvas.width = $(window).width();
        //paintGame();
        paintGame();
    }
});

var map;
var infowindow;

function getLocations(lat, long) {
    var pyrmont = new google.maps.LatLng(lat, long);

    map = new google.maps.Map(document.getElementById('map'), {
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: pyrmont,
        zoom: 15
    });

    var request = {
        location: pyrmont,
        radius: 2500
    };
    infowindow = new google.maps.InfoWindow();
    var service = new google.maps.places.PlacesService(map);
    service.search(request, callback);
}

function callback(results, status) {
    if (status == google.maps.places.PlacesServiceStatus.OK) {
        for (var i = 0; i < results.length; i++) {
            var loc = util.toECEF(results[i].geometry.location.Xa,results[i].geometry.location.Ya);
            /*Trees*/
            var s = 50;
            var place = new MapMarkerObject(results[i].name);
            place.w = s;
            place.h = s * 2;
            place.x = loc[0];
            place.y = loc[1];
            place.bearing = 0;
            objs.push(place);
        }
    }
}

function init() {
        ss = new SpriteSheet(document.getElementById("web_001"), 3, 4);
        $("#web_001").css("display", "none");
        
        // Google Places Test
        getLocations(_location[4], _location[5]);

        canvas = document.createElement('canvas');
        canvas.top = 0;
        canvas.left = 0;
        canvas.height = $(window).height();
        canvas.width = $(window).width();
        canvas.style.position = "absolute";
        canvas.style.zIndex = 1;

        backCanvas = document.createElement('canvas');
        backCanvas.top = 0;
        backCanvas.left = 0;
        backCanvas.height = $(window).height();
        backCanvas.width = $(window).width();
        backCanvas.style.position = "absolute";
        backCanvas.style.zIndex = 0;

        document.getElementsByTagName('article')[0].appendChild(backCanvas);
        document.getElementsByTagName('article')[0].appendChild(canvas);

        ctx = canvas.getContext("2d");
        bctx = backCanvas.getContext("2d");

        sky = new SkyPainter();
        ground = new GroundPainter();

        mist = new MistObject(1000, 0.2);
        objs.push(mist);

        pctx = new PaintingContext();
        pctx.viewPort = new ViewPort(6, util._90, util._45);
        pctx.camera = new Placement(); //new Camera();

        pctx.camera.x = _location[0];
        pctx.camera.y = _location[1];
        pctx.camera.w = 3.0;
        pctx.camera.h = 6.25;
        pctx.camera.bearing = _bearing ? _bearing : util._90;

        inputHandler = new Input(pctx.camera);

        ///*Trees*/
        for (var j = 0; j < 200; j++) {

            var s = util.random(15, 70);
            var tree = new TreeObject();
            tree.w = s;
            tree.h = s;
            tree.x = util.random(pctx.camera.x - 3600, pctx.camera.x + 3600);
            tree.y = util.random(pctx.camera.y - 3600, pctx.camera.y + 3600);
            tree.bearing = util.radians(util.random(180));
            objs.push(tree);
        }

        pctx.backLayer = bctx;
        pctx.frontLayer = ctx;

        runTests();
        paintGame();
        setInterval(paintGame, 100);
        isSensorReady = true;
    }

function paintGame() {

    pctx.frontLayer.clearRect(0, 0, canvas.width, canvas.height);
	
	pctx.viewPort.setPixelSize(canvas.width, canvas.height);
	pctx.gameObject = null;
	sky.paint(pctx);
	ground.paint(pctx);

	//pctx.gameObject = mist;
	//mist.getPainter().paint(pctx);
	
	for (var i = 0; i < objs.length; i++) objs[i].updateLocation(pctx);
	util.orderByDistance(objs);
	for (var i = 0; i < objs.length; i++) {
	    pctx.gameObject = objs[i];
	    objs[i].getPainter().paint(pctx);
	}

	
	pctx.frontLayer.fillStyle = "#000";
	pctx.frontLayer.fillText('X: ' + _location[0] + ' Y: ' + _location[1] + ' Z: ' + _location[2] + ' T: ' + _location[3], 10, 10);

	if (_location) {
	    pctx.camera.x = _location[0];
	    pctx.camera.y = _location[1];
	}

	if (_bearing) {
	    pctx.camera.bearing = util.radians(_bearing);
	    pctx.frontLayer.fillText('B: ' + _bearing, 10, 20);
	}

	//if (sillyRotate >= 60) { sillyRotate = 0; sillyRotateInterval *= -1;}
	//else sillyRotate += sillyRotateInterval;
	//pctx.camera.turn(util.radians(sillyRotateInterval));
	//ss.draw(pctx.frontLayer);
	//ss.tick();
	//rotationCount++;
	//if ((rotationCount % 10) == 0) {
	//	ss.rotate(1);
	//	ss.sizeMultiplier += .1;
	//}
	
	//pctx.frontLayer.setFillStyle = "black";
	//pctx.frontLayer.fillRect(50,50,50,50);
	
	//ctx.fillRect(0, 0, 500, 500);
}

function testies() {
	//ctx.fillRect(0, 0, 500, 500);
	//paintGame();
	//if (rotationCount > 0) pctx.frontLayer.clearRect(0, 0, canvas.width, canvas.height);

	/*ss.draw(pctx.frontLayer);
	ss.tick();
	//if (ss.col <= 2)
	//window.setTimeout(testies, 500);
	rotationCount++;
	if ((rotationCount % 10) == 0) {
		ss.rotate(1);
		ss.sizeMultiplier += .1;
	}*/
}

function runTests() {
	var p = new Point2D(2,-3);
	var s = new Size(12, 6);
	var r = new Rectangle(p, s);
	if (r.getBottom() != -9) throw new Error("r.getBottom() != -9 ==>" + r.getBottom());
	if (r.getRight() != 14) throw new Error("r.getRight() != 14 ==>" + r.getRight());
	if (r.contains(new Point2D(1, -2))) throw new Error("r.contains(new Point2D(1, -2))");
	if (!r.contains(new Point2D(4, -4))) throw new Error("!r.contains(new Point2D(4, -4))");
	if (r.contains(new Rectangle(1, 1, 12, 6))) throw new Error("r.contains(new Rectangle(1, 1, 12, 6))");
	if (!r.contains(new Rectangle(2,-3, 12, 6))) throw new Error("r.contains(new Rectangle(2,-3, 12, 6))");
	if (r.intersects(new Rectangle(12, 12, 12, 6))) throw new Error("r.intersects(new Rectangle(12, 12, 12, 6))");
	if (!r.intersects(new Rectangle(-2,-3, 12, 6))) throw new Error("r.intersects(new Rectangle(2,-3, 12, 6))");
	
	var vp = new ViewPort(6, util._90, util._45);
}

